//
//  RCMapAnnotation.m
//  ReCite
//
//  Created by Miles on 10-05-14.
//  Copyright 2010 Miles Thorogood
//  greenmeat.net. All rights reserved.
//

#import "RCMapAnnotation.h"

@interface RCMapAnnotation() 

-(void) populateAdAtributes ;
-(void) loadAnnotation ;
@end

@implementation RCMapAnnotation
@synthesize title, subtitle, text, image, player, dataDictionary ;
@synthesize thisAd ;
@synthesize coordinate ;

-(id) initWithDictionary:(NSMutableDictionary *) dictionary {
	self = [super init] ;
	if (self) {
		self.dataDictionary = dictionary ;
		[self populateAdAtributes] ;
	}
	return self ;
}



-(void) populateAdAtributes {
	
	if(![[dataDictionary objectForKey:@"audio"] isEqualToString:@""]) {
		//printf("HERE A/n");
		thisAd.audioUrl = [NSURL URLWithString:[dataDictionary objectForKey:@"audio"]];
	}
	if(![[dataDictionary objectForKey:@"image"] isEqualToString:@""]) {
		//printf("HERE B/n");
		thisAd.imageUrl = [NSURL URLWithString:[dataDictionary objectForKey:@"image"]];
	}
	if(![[dataDictionary objectForKey:@"text"] isEqualToString:@""]) {
		//printf("HERE C/n");
		thisAd.textUrl = [NSURL URLWithString:[dataDictionary objectForKey:@"text"]];
		
	}
	if(![[dataDictionary objectForKey:@"ad"] isEqualToString:@""]) {
		//printf("HERE D/n");
		thisAd.ad_identifier = [dataDictionary objectForKey:@"ad"] ;
	}
	//if(![[dataDictionary objectForKey:@"location"] isEqualToString:@""]) {
		thisAd.location = [dataDictionary objectForKey:@"location"] ;
	//}
	
	[self loadAnnotation] ;
}

-(void) loadAnnotation {
	NSError *error;
	NSURL *testUrl = [NSURL URLWithString:@""] ;
	
	if(![thisAd.audioUrl isEqual:testUrl]) {
		self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:thisAd.audioUrl error:&error];
	}
	if(![thisAd.imageUrl isEqual:testUrl]) {
		NSData *data = [NSData dataWithContentsOfURL:thisAd.imageUrl];
		self.image = [[UIImage alloc] initWithData:data] ;	
	}
	if(![thisAd.textUrl isEqual:testUrl]) {
		text = [NSString stringWithContentsOfURL:[NSURL URLWithString:[dataDictionary objectForKey:@"text"]] encoding:NSASCIIStringEncoding error:nil] ;
		self.title =text ;
		self.subtitle = @"gookaly gob";//text ;
	}
	
	//if(![[dataDictionary objectForKey:@"location"] isEqualToString:@""]) {
		
		CLLocation *loc = [dataDictionary objectForKey:@"location"] ;
		coordinate.latitude = loc.coordinate.latitude ;
		coordinate.longitude = loc.coordinate.longitude ;
	//}
	
}

@end
